#include "stdafx.h"
#include "ineedthis.h"


// Called in wm_paint
void MyEdit::drawRect(HDC hdc)
{
	// Draw a rectangle that borders the whole editbox
	Rectangle(hdc, m_rc.left, m_rc.top, m_rc.right, m_rc.bottom);

	// Make a rectangle that is inside the editbox for the text
	RECT rccopy;
	memset(&rccopy, 0, sizeof(RECT));
	memcpy(&rccopy, &m_rc, sizeof(RECT));

	rccopy.top++;
	rccopy.bottom++;
	rccopy.left++;

	//DrawText(hdc, m_string.c_str(), m_string.length(), &rccopy, NULL);

	std::list<MyLine *>::iterator lineiter;
	for (lineiter = m_lines.begin(); lineiter != m_lines.end(); lineiter++)
	{
		std::list<MyChar *> *manylines = (*lineiter)->getChars();
		std::list<MyChar *>::iterator manylineiter;
		for (manylineiter = manylines->begin(); manylineiter != manylines->end(); manylineiter++)
		{
			(*manylineiter)->drawChar(hdc);
		}

	}


}

void MyEdit::setRect(RECT *rc)
{
	memset(&m_rc, 0, sizeof(RECT));		// Set member rect to zero memory
	memcpy(&m_rc, rc, sizeof(RECT));	// Copy memory of argument rect to member rect
}

void MyEdit::putChar(char ch)
{
	std::list<MyLine *>::iterator lineiter;

	MyLine *myline;
	MyChar *mychar = new MyChar();
	mychar->setChar(ch);
	RECT rc, calcrc;
	HDC hdc;
	hdc = GetDC(m_parenthwnd);

	memset(&rc, 0, sizeof(RECT));
	memset(&calcrc, 0, sizeof(RECT));

	DrawText(hdc, &ch, 1, &calcrc, DT_CALCRECT);
	mychar->setWidth(calcrc.right - calcrc.left);



	rc.left = m_lastrc.right;
	rc.right = m_lastrc.right + mychar->getWidth();
	rc.top = m_lastrc.top;
	rc.bottom = m_lastrc.top + calcrc.bottom;
	mychar->setRect(&rc);
	
	myline = m_lines.back();
	std::list<MyChar *> *somechars = myline->getChars();
	if (m_index < somechars->size())
	{
		std::list<MyChar *>::iterator chariter;
		int j = 0;
		for (chariter = somechars->begin(); j <= m_index; chariter++, j++)
		{
			if (j == m_index)
			{
				memcpy(&rc, (*chariter)->getRect(), sizeof(RECT));
				rc.right = rc.left + mychar->getWidth();
				mychar->setRect(&rc);
				somechars->insert(chariter, mychar);
				

			}
		}
		m_index++;
		if (rc.right > m_rc.right - 1)
		{
			m_line++;
			int height = rc.bottom - rc.top;
			rc.top = rc.top + height;
			rc.bottom = rc.bottom + height;
			SetCaretPos(m_rc.left+1, rc.top);
		}
		m_lastrc.right = rc.right;
		SetCaretPos(rc.right, rc.top);
		j = 0;
		for (chariter = somechars->begin(); chariter != somechars->end(); chariter++, j++)
		{
			if (j >= m_index)
			{
				memcpy(&rc, (*chariter)->getRect(), sizeof(RECT));
				rc.left = rc.left + mychar->getWidth();
				rc.right = rc.right + mychar->getWidth();
				
				std::list<MyLine *>::iterator lineiter;
				{
					MyLine *someline;
					someline = m_lines.back();
					if (someline == myline)
					{
						someline = new MyLine();
						someline->initialize(line_index);
						m_lines.push_back(someline);
					}

					m_lines.push_back(someline);
				}

				(*chariter)->setRect(&rc);
			}
		}
	}
	else
	{
		myline->addChar(mychar);
		m_index++;
	}

	memcpy(&m_lastrc, &rc, sizeof(RECT));
}

void MyEdit::setParent(HWND hwnd)
{
	m_parenthwnd = hwnd;

}

void MyEdit::setLastRect(RECT *rc)
{
	memset(&m_lastrc, 0, sizeof(RECT));
	memcpy(&m_lastrc, rc, sizeof(RECT));
}

MyEdit::MyEdit()
	:	m_index(0)
{
}

void MyEdit::lButtonDown(int x, int y)
{
	RECT rc;	// Pointer to the rect of a char
	memset(&rc, 0, sizeof(RECT));
	int i;
	MyLine *myline;
	myline = m_lines.back();
	std::list<MyChar *> *somechars = myline->getChars();

	std::list<MyChar *>::iterator iter;			// Iterator that goes through the list of chars
	for (iter = somechars->begin(), i = 0; iter != somechars->end(); iter++, i++)		// for loop
	{
		memcpy(&rc, (*iter)->getRect(), sizeof(RECT));
		if (rc.right > x)					// If the right of the rect is greater than the
												// location of click
		{
			int width = rc.right - rc.left;		// Find width of rect
			int left = x - rc.left;				// Find out how far location of click is from
													// left of rect
			int right = rc.right - x;				// Find out how far location of click is from
													// right of rect
		
			if (left > right)						// If (x - rc->left) > (rc->right - x)
													// which means if x is closer to right of rect
			{
				SetCaretPos(rc.right, rc.top);
				m_lastrc.right = rc.right;
				m_index = i+1;
				break;
			}
			else
			{
				SetCaretPos(rc.left, rc.top);
				m_lastrc.right = rc.left;
				m_index = i;
				break;
			}
		}

	}
	if (iter == somechars->end())
	{
		SetCaretPos(rc.right, rc.top);
	}


}

void MyEdit::initialize()
{
	m_lastrc.top = m_rc.top+1;
	m_lastrc.left = m_rc.left+1;
	m_lastrc.right = m_rc.left+1;
	m_lastrc.bottom = m_rc.top+1;

	MyLine *myline;
	myline = new MyLine();
	myline->initialize(0);
	m_lines.push_back(myline);

	m_line = 0;
}
